package com.visikard.ui.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.visikard.application.UIMessage;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.ForgotPassTask;
import com.visikard.ui.R;

public class SendEmailRecoveryPass extends BaseFragmentActivity implements OnClickListener {

	public static final String KEY_EMAIL = "key_recovery_email";

	private EditText edtSendMail;
	private TextView btnSendMail;
	private TextView btnCancel;
	private LinearLayout linearSendMail;
	private Context mContext;

	public static final String IS_RESET_PIN_WALLET = "forgotpinwallet";

	// Business Object
	private BaseTask mAsyncTask;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			overridePendingTransition(R.anim.anim_slide_in_from_left, R.anim.anim_slide_out_to_right);
			return true;
		}
		else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.send_forgot_email_pass);
		mContext = this;
		innitControl();
	}

	private void innitControl() {
		linearSendMail = (LinearLayout) findViewById(R.id.ly_forpass_email);
		linearSendMail.setOnClickListener(this);

		edtSendMail = (EditText) findViewById(R.id.edtsendemail);

		String email = getIntent().getStringExtra(KEY_EMAIL);
		if (email != null) {
			edtSendMail.setText(email);
			edtSendMail.setSelection(0, email.length());
		}

		btnSendMail = (TextView) findViewById(R.id.btnsendmail);
		btnSendMail.setOnClickListener(this);

		btnCancel = (TextView) findViewById(R.id.btncancel);
		btnCancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		InputMethodManager imd;

		switch (v.getId()) {
			case R.id.btnsendmail:

				imd = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
				imd.hideSoftInputFromWindow(linearSendMail.getApplicationWindowToken(), 0);

				String mEmail = edtSendMail.getText().toString();
				if (mEmail.length() <= 0 || mEmail == "") {
					showDialogEmail();
					return;
				}
				else {
					sendEmailLogin(mEmail);
				}
				break;
			case R.id.btncancel:
				imd = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
				imd.hideSoftInputFromWindow(linearSendMail.getApplicationWindowToken(), 0);
				finish();
				overridePendingTransition(R.anim.anim_slide_in_from_left, R.anim.anim_slide_out_to_right);
				break;
			case R.id.ly_forpass_email:
				imd = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
				imd.hideSoftInputFromWindow(linearSendMail.getApplicationWindowToken(), 0);
				break;
			default:
				break;
		}
	}

	private void sendEmailLogin(String email) {
		mAsyncTask = new ForgotPassTask(mHandler);
		mAsyncTask.execute(email);
	}

	private void showDialogEmail() {
		final AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
		alert.setIcon(android.R.drawable.ic_dialog_info);
		alert.setTitle(getResources().getString(R.string.msg_enter_email));
		alert.setPositiveButton(getResources().getString(R.string.literal_ok), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
				dialog.cancel();
			}
		});
		alert.show();
	}

	// Handle message method
	@Override
	protected boolean handleMessage(Message msg) {
		switch (msg.what) {
			case UIMessage.MSG_IN_VALID_EMAIL:
				Toast.makeText(mContext, getResources().getString(R.string.msg_invalid_email), Toast.LENGTH_SHORT)
						.show();
				edtSendMail.setTextColor(Color.parseColor("#000000"));
				mHandler.removeMessages(UIMessage.MSG_IN_VALID_EMAIL);
				return true;
			case UIMessage.MSG_SEND_EMAIL_FAILURE:
				AlertDialog.Builder alert1 = new AlertDialog.Builder(mContext);
				String msg_failure = getResources().getString(R.string.msg_reset_pass_failure);
				alert1.setIcon(android.R.drawable.ic_dialog_info);
				alert1.setTitle(getResources().getString(R.string.dialog_title_warrning));
				alert1.setMessage(msg_failure);

				alert1.setPositiveButton(getResources().getString(R.string.literal_ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int whichButton) {
								sendEmailLogin(edtSendMail.getText().toString());
							}
						});
				alert1.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
						// finish();
					}
				}).create();
				alert1.show();
				mHandler.removeMessages(UIMessage.MSG_SEND_EMAIL_FAILURE);
				return true;
			case UIMessage.MSG_SEND_EMAIL_SUCCESS:
				mHandler.removeMessages(UIMessage.MSG_SEND_EMAIL_SUCCESS);
				String msg_success = getResources().getString(R.string.msg_reset_pass_success);
				AlertDialog alert = new AlertDialog.Builder(mContext).setIcon(android.R.drawable.ic_dialog_info)
						.setTitle(getResources().getString(R.string.login)).setMessage(msg_success)
						.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.cancel();
								finish();
								overridePendingTransition(R.anim.anim_slide_in_from_left,
										R.anim.anim_slide_out_to_right);
							}
						}).create();
				alert.show();
				return true;
			case UIMessage.MSG_EMAIL_NOT_EXISTS:
				mHandler.removeMessages(UIMessage.MSG_EMAIL_NOT_EXISTS);
				AlertDialog.Builder alertMail = new AlertDialog.Builder(mContext);
				alertMail.setIcon(android.R.drawable.ic_dialog_info);
				alertMail.setTitle(getResources().getString(R.string.dialog_title_warrning));
				alertMail.setMessage(getResources().getString(R.string.msg_enter_email_not_exists));

				alertMail.setPositiveButton(getResources().getString(R.string.literal_ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int whichButton) {
								dialog.cancel();
							}
						}).create();
				alertMail.show();
				return true;
			case UIMessage.MSG_EMAIL_INTERNET_ERROR:
				return true;
		}
		return super.handleMessage(msg);
	}
}
